'use strict'
var config = require('./config')

function Wheel(r, index = 0){
    this.create(r, index)
}


//生成
Wheel.prototype.create = function(r, index = 0){
    //周长
    var length = 2 * config.PI * r
    //可容纳的单位数量
    var units = Math.floor(length / config.UNIT)
    var hole = Array(units).fill(0)
    this.r = r
    this.length = length
    this.units = units
    this.start = 0
    this.hole = hole
    this.holes = 0
    this.holeMap = []
    this.users = 0
    this.site = {}
    this.index = index
}

//开洞
Wheel.prototype.openHole = function(num){
    if(this.holes == this.units) return
    if(config.HOLE_HOLES){
        for(var i = 0; i < num; i++){
            var index = Math.floor(Math.random() * this.units)
            if(this.hole[index] == 0){
                this.holes ++
                this.holeMap.push(index)
            }
            this.hole[index] = 1
        }
    }else{
        //必须开洞成功
        if(num + this.holes > this.units) num = this.units - this.holes
        for(var i = 0; i < num; i++){
            do{
                var index = Math.floor(Math.random() * this.units)
            }while(this.hole[index] != 0)
            this.holes ++
            this.holeMap.push(index)
            this.hole[index] = 1
        }
    }
}

//检查当前位置是否破洞了.是返回true 否则返回false
Wheel.prototype.checkHole = function(index){
    var idx = (this.start + parseInt(index)) % this.units
    return this.hole[idx]? true: false
}

Wheel.prototype.checkOut =  function(index){
    this.users --
    return delete this.site[index]
}

//接受上一个轮子过来的用户, 安置一个index
Wheel.prototype.getIn = function(user){
    var index = this.getSite()
    //找不到位置坐,转给下一个轮子
    if(index === false) return this.next.getIn(user)
    this.site[index] = user
    this.users ++
    user.addHole(this.index + 1)
    user.wheel = this
}

//获取座位, 必须获取到一个有效的座位
Wheel.prototype.getSite = function(){
    //坐满了 返回
    if(this.units - this.holes <= this.users) return false
    var index = this.getRandom(config.FORCE_IN)
    if(index === false) return false
    while(this.site[index]){
        index = this.getRandom()
    }
    return index
}

Wheel.prototype.getRandom = function(force = false){
    if(!force)
    return Math.floor(Math.random() * this.units)
    if(this.holes == this.units) return false
    var index = false
    while(index === false){
        index = Math.floor(Math.random() * this.units)
        if(this.checkHole(index)) index = false
    }
    return index
}
//转动
Wheel.prototype.round = function(){
    var num = 0
    //收集投票
    for(var i in this.site){
        if(this.site[i].vote <= 0) this.site[i].vote = 1
        num += this.site[i].vote
        this.site[i].vote = 0
    }
    this.votes = num
    //转动
    this.start = (this.start + num) % this.units
    //检查所有人是否可以漏下去
    for(var i in this.site){
        if(this.checkHole(i)){
            if(this.next){
                this.next.getIn(this.site[i])
            }else{
                this.site[i].wheel = null
            }
            this.checkOut(i)
        }
    }
}
module.exports = Wheel

